//
//  PKLoginViewController.m
//  pantaKills
//
//  Created by 湛凯杰 on 16/8/3.
//  Copyright © 2016年 湛凯杰. All rights reserved.
//

#import "PKLoginViewController.h"
#import "PKwelcomeViewController.h"
#import "PKforgotpasswordViewController.h"
#import "PKMainViewController.h"
@interface PKLoginViewController ()<UITextFieldDelegate>{
    UIImageView * imageViewOfHeard;
    UIImageView * imageViewOfTitle;
    UITextField * textFieldOfaccount;
    UITextField * textFieldOfpassword;
    UIButton * btnOfLogin;
    UIButton * btnOfForgotOfPassword;
    UILabel * labelOftag;
    UIButton * btnOfback;
}

@end

@implementation PKLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=MAIN_BACKCOLOR;
    [self addValue];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    //增加监听，当键退出时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 布局
-(void)addValue{
    if (!imageViewOfHeard) {
        imageViewOfHeard=[[UIImageView alloc]init];
        NSMutableArray* arrayOfimage=[NSMutableArray array];
        [arrayOfimage addObject:[UIImage imageNamed:@"login_heard1"]];
        [arrayOfimage addObject:[UIImage imageNamed:@"login_heard2"]];
        [arrayOfimage addObject:[UIImage imageNamed:@"login_heard3"]];
        [arrayOfimage addObject:[UIImage imageNamed:@"login_heard4"]];
        imageViewOfHeard.animationImages=arrayOfimage;
        imageViewOfHeard.animationDuration=1;
        [imageViewOfHeard startAnimating];
    }
    if (!imageViewOfTitle) {
        imageViewOfTitle=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"login_tit"]];
    }
    if (!textFieldOfaccount) {
        textFieldOfaccount=[[UITextField alloc]initTypeOfLonginWithPlaceholder:@"username" AndSecureTextEntry:NO AndCornerRadius:20.0];
        textFieldOfaccount.delegate=self;
        textFieldOfaccount.tag=101;
    }
    if (!textFieldOfpassword) {
        textFieldOfpassword=[[UITextField alloc]initTypeOfLonginWithPlaceholder:@"password" AndSecureTextEntry:YES AndCornerRadius:20];
        textFieldOfpassword.delegate=self;
        textFieldOfpassword.tag=102;
    }
    if (!btnOfLogin) {
        btnOfLogin = [[UIButton alloc]initTypeOfLonginWithTitle:@"Log In" AndCornerRadius:20.0];
        [btnOfLogin setBackgroundImage:[UIImage imageNamed:@"btn_login"] forState:UIControlStateNormal];
        [btnOfLogin addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
        
    }
    if (!btnOfForgotOfPassword) {
        btnOfForgotOfPassword=[[UIButton alloc]init];
        btnOfForgotOfPassword.backgroundColor=[UIColor clearColor];
        [btnOfForgotOfPassword setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btnOfForgotOfPassword setTitle:@"Forgot your password?" forState:UIControlStateNormal];
        [btnOfForgotOfPassword addTarget:self action:@selector(pushForgotPassword) forControlEvents:UIControlEventTouchUpInside];
    }
    if (!labelOftag) {
        labelOftag=[[UILabel alloc]init];
        labelOftag.textAlignment=NSTextAlignmentCenter;
        labelOftag.font=[UIFont systemFontOfSize:13.0];
        labelOftag.text = @"Copying@2017-2033 HuBei Guangshui ZanKj";
    }
    if (!btnOfback) {
        btnOfback=[[UIButton alloc]init];
        [btnOfback addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        [btnOfback setBackgroundImage:[UIImage imageNamed:@"fullplayer_icon_back"] forState:UIControlStateNormal];
    }
    [self addViews];
}
-(void)back{
    [textFieldOfpassword resignFirstResponder];
    [textFieldOfaccount resignFirstResponder];
    [self presentViewController:[PKwelcomeViewController new] animated:YES completion:^{
        
    }];
    
}
-(void)login{
    [textFieldOfaccount resignFirstResponder];
    [textFieldOfpassword resignFirstResponder];
    if (textFieldOfaccount.text.length!=11||textFieldOfpassword.text.length<=4) {
        [SVProgressHUD showSuccessWithStatus:@"格式有误"];
    }else{
        PKMainViewController * mainBarVC=[[PKMainViewController alloc]init];
        [self presentViewController:mainBarVC animated:YES completion:^{
            
        }];
    }
    
}
-(void)pushForgotPassword{
    PKforgotpasswordViewController * forgotpasswordVC =[[PKforgotpasswordViewController alloc]init];
    [textFieldOfaccount resignFirstResponder];
    [textFieldOfpassword resignFirstResponder];
    [self presentViewController:forgotpasswordVC animated:YES completion:^{
    }];
    
}
-(void)addViews{
    [self.view addSubview:imageViewOfHeard];
    [self.view addSubview:imageViewOfTitle];
    [self.view addSubview:textFieldOfaccount];
    [self.view addSubview:textFieldOfpassword];
    [self.view addSubview:btnOfLogin];
    [self.view addSubview:btnOfForgotOfPassword];
    [self.view addSubview:labelOftag];
    [self.view addSubview:btnOfback];
    [imageViewOfHeard mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(100);
        make.centerX.equalTo(self.view.mas_centerX);
        make.height.mas_equalTo(70);
        make.width.mas_equalTo(70);
        
    }];
    [imageViewOfTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(50);
        make.top.equalTo(imageViewOfHeard.mas_bottom).offset(10);
        make.centerX.equalTo(self.view.mas_centerX);
    }];
    [textFieldOfaccount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.view.mas_trailing).offset(-30);
        make.leading.equalTo(self.view.mas_leading).offset(25);
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(imageViewOfTitle.mas_bottom).offset(30);
        make.height.mas_equalTo(40);
    }];
    [textFieldOfpassword mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.view.mas_trailing).offset(-30);
        make.leading.equalTo(self.view.mas_leading).offset(25);
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(textFieldOfaccount.mas_bottom).offset(30);
        make.height.mas_equalTo(40);
    }];
    [btnOfLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.view.mas_trailing).offset(-30);
        make.leading.equalTo(self.view.mas_leading).offset(25);
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(textFieldOfpassword.mas_bottom).offset(30);
        make.height.mas_equalTo(40);
    }];
    [btnOfForgotOfPassword mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.view.mas_trailing).offset(-45);
        make.leading.equalTo(self.view.mas_leading).offset(45);
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(btnOfLogin.mas_bottom).offset(60);
        make.height.mas_equalTo(30);
    }];
    [labelOftag mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view);
        make.trailing.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).offset(-14);
        
    }];
    [btnOfback mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(40);
        make.leading.equalTo(self.view.mas_leading).offset(20);
        make.width.height.mas_equalTo(35);
    }];
}

#pragma mark - textfidel代理

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [textFieldOfpassword resignFirstResponder];
    [textFieldOfaccount resignFirstResponder];
}

- (void)keyboardWillShow:(NSNotification *)aNotification
{
    //获取键盘的高度
    NSDictionary *info = [aNotification userInfo];
    //取出动画时长
    CGFloat animationDuration=[[info valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];;
    //取出键盘位置大小
    CGRect keyboardBounds = [info[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect keyboardRect = [self.view convertRect:keyboardBounds toView:nil];
    CGFloat keyboardHeight = keyboardBounds.size.height;
    UIViewAnimationOptions options = (UIViewAnimationOptions)[[info valueForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue] << 16;
    [UIView animateKeyframesWithDuration:animationDuration delay:0 options:options animations:^{
        self.view.transform = CGAffineTransformMakeTranslation(0, -keyboardHeight + 64);
    } completion:nil];
}

//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification
{
    NSDictionary *info = [aNotification userInfo];
    //取出动画时长
    CGFloat animationDuration = [[info valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    //下移动画options
    UIViewAnimationOptions options = (UIViewAnimationOptions)[[info valueForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue] << 16;
    //回复动画
    [UIView animateKeyframesWithDuration:animationDuration delay:0 options:options animations:^{
        self.view.transform = CGAffineTransformIdentity;
    } completion:nil];
}
-(void)animateKeyframesWithDuration{
    
}


@end
